using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using Scb.Framework;

namespace WinAppRobot
{
  public partial class Form1 : Form
  {

    tblOpicsTransactionCollection collection = new tblOpicsTransactionCollection();
    int tranNo = 0;
    public Form1()
    {
      InitializeComponent();
    }

    #region Event

    private void btnStart_Click(object sender, EventArgs e)
    {
      lbStatus.Text = "Robot is running...";
      lbTranNo.Refresh();
      timer1.Start();
      btnStart.Enabled = false;
      btnStop.Enabled = true;
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
      lbStatus.Text = "Robot is stopped!";
      lbTranNo.Refresh();
      timer1.Stop();
      btnStart.Enabled = true;
      btnStop.Enabled = false;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
      if (MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        this.Close();
      }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      Run();
    }

    #endregion

    #region Helper

    public bool AutoInsertOpics(tblOpicsTransaction opicsTran)
    {
      return true;
    }

    private void Run()
    {
      collection.AddRange(tblOpicsTransactionService.GettblOpicsTransactionListByStatus(1));
      if (collection.Count == 0)
      {
        lbStatus.Text = "Robot is sleeping...";
        lbTranNo.Refresh();
        return;
      }
      lbStatus.Text = "Robot is running...";
      lbTranNo.Refresh();
      while (collection.Count > 0)
      {
        tblOpicsTransaction opicsTran = (tblOpicsTransaction)collection[0];

        if (!AutoInsertOpics(opicsTran))
           return;

         opicsTran = tblOpicsTransactionService.GettblOpicsTransaction(opicsTran.ID);
         opicsTran.Status = 2; //inserted
         opicsTran.RunningDate = DateTime.Now;

        tblOpicsTransactionService.UpdatetblOpicsTransaction(opicsTran);
        collection.RemoveAt(0);
        tranNo++;
        lbTranNo.Text = "Transaction inserted: " + tranNo.ToString();
        lbTranNo.Refresh();
      }
     
    }
    #endregion

    


  }
}